Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
Error: Malformed array literal, trying to pass in an array into TEXT[] column

So I have an array that has been created in Node.JS by pushing elements to an array, that I'm then trying to set to a PostgreSQL Text[] column. So it's something like this:

var hours = []
for (x of hourStrings) {
  hours.push(x) // Here, x is something like {"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"}
}

My element that I'm trying to put into a Postgres column looks like this:

{{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"}}

and the error I'm getting is this:

error: malformed array literal: "{{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"}}"

I'm querying with this:

const client = await pool.connect()
await client.query(`INSERT INTO tableName (hoursColumn) VALUES($1) ON CONFLICT DO NOTHING`, [hours]);

I don't see where the error is in the object. Not sure how to fix this, any help is much appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

For this instead of a for loop I used foreach and it seemed to work for me! I also changed up hourStrings.


let hourStrings = ["2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"]

let  hours = []

hourStrings.forEach(element => {
    hours.push(element)
});


console.log(hours)
about 4 years ago · Juan Pablo Isaza Relatório

0

Problem is in empty element {} you have in text.

Additionally each element of the array should have the same dimension

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda